package com.qire.manhua.model.viewModel.impl;

import android.graphics.Bitmap;
import android.os.Build;
import android.view.View;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;

import com.qire.common.SummerApp;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.collect.ListUtil;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.ReferBinder;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerBannerAdapter;
import com.qire.common.widget.adapter.auto.SummerComplexBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.RecommendBoysController;
import com.qire.manhua.dal.service.RecommendBoysService;
import com.qire.manhua.databinding.LayoutHomeRecommendBoysBannerItemBinding;
import com.qire.manhua.databinding.LayoutHomeRecommendBoysSlideZoneBinding;
import com.qire.manhua.model.entity.AppAdvert;
import com.qire.manhua.view.RoundedImageView;
import com.qire.manhua.view.drawable.RoundedDrawable;
import com.qire.manhua.view.filter.BlurBuilder;
import com.qire.manhua.view.filter.FastBlur;

import java.util.ArrayList;

/**
 * 首页-推荐页-男生
 */
public class RecommendBoysViewModel extends BaseViewModel<IViewModel> {

    private final RecommendBoysService boysService = new RecommendBoysService(this);

    private final BoysSlideZoneViewModel slideZoneViewModel = new BoysSlideZoneViewModel();

    private final ArrayList<AutoVariable> moduleList = new ArrayList();
    private final SummerComplexBindAdapter moduleAdapter = new SummerComplexBindAdapter(moduleList);


    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(RecommendBoysController controller) {
        refreshUI();
        slideZoneViewModel.setEventProcess(controller);
        moduleAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        slideZoneViewModel.setEventProcess(null);
        moduleAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    // region    #业务

    public void loadBoysModuleData() {
        // todo: 处理启用偏好
        boysService.loadPageData();
    }

    public void clearBannerList() {
        slideZoneViewModel.clearBannerList();
    }

    public void addBannerItemViewModel(AppAdvert appAdvert) {
        slideZoneViewModel.addBannerItemViewModel(appAdvert);
    }

    public void refreshBannerAdapter() {
        slideZoneViewModel.refreshBannerAdapter();
    }

    public void clearModuleList() {
        moduleAdapter.notifyItemRangeRemoved(1, moduleList.size() -1);
        moduleList.clear();
        moduleList.add(slideZoneViewModel);
    }
    public void addModule(AutoVariable[] modules) {
        if(ObjectUtil.nonNull(modules)) {
            for(AutoVariable module : modules) {
                if(ObjectUtil.nonNull(module)) {
                    moduleList.add(module);
                }
            }
        }
    }
    public void refreshModuleAdapter() {
        moduleAdapter.notifyItemChanged(0, "full");
    }

    // endregion #业务


    // region #UI操作

    public void refreshUI() {
        refreshEmptyDataUI();
    }

    public void refreshEmptyDataUI() {
        boolean isEmpty = false;
        set("errorLayoutPanelVisible", isEmpty ? View.VISIBLE : View.GONE);
    }

    public void changeBannerBackground(int index) {
        slideZoneViewModel.changeBannerBackground(index);
    }

    // endregion

    // region #UI属性绑定

    public MutableLiveData<Integer> getErrorLayoutPanelVisible() {
        return get("errorLayoutPanelVisible");
    }

    public Adapter getModuleAdapter() {
        return moduleAdapter;
    }

    public LayoutManager getModuleLayoutManager() {
        return  new LinearLayoutManager(null, LinearLayoutManager.VERTICAL, false);
    }

    // endregion

    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_boys_slide_zone)
    public class BoysSlideZoneViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendBoysSlideZoneBinding> {

        private final ArrayList<BoysBannerItemViewModel> bannerList = new ArrayList<>();
        private final SummerBannerAdapter bannerAdapter = new SummerBannerAdapter(bannerList, R.layout.layout_home_recommend_boys_banner_item);

        private Bitmap bannerBackground;

        @Override
        public void bindVariable(LayoutHomeRecommendBoysSlideZoneBinding viewBind) {
            viewBind.setBoysSlideZoneViewModel(this);
        }

        public void setEventProcess(AutoVariable autoVariable) {
            bannerAdapter.setEventProcess(autoVariable);
        }

        public void clearBannerList() {
            bannerAdapter.notifyItemRangeRemoved(0, bannerList.size());
            bannerList.clear();
        }

        public void addBannerItemViewModel(AppAdvert appAdvert) {
            bannerList.add(new BoysBannerItemViewModel(appAdvert));
        }

        public void refreshBannerAdapter() {
            bannerAdapter.notifyItemChanged(0, "full");
        }

        public void changeBannerBackground(int index) {
            // todo：临时修复banner无限循环时出现的溢出问题，零时调整，在banner无限循环修复后，应该删除下面这句话。
            index = ListUtil.isEmpty(bannerList) ? 0 : index % bannerList.size();

            BoysBannerItemViewModel bannerItemViewModel = bannerList.get(index);
            RoundedImageView imageView = bannerItemViewModel.roundedImageView;
            if(ObjectUtil.nonNull(imageView)) {
                RoundedDrawable roundedDrawable = (RoundedDrawable) imageView.getDrawable();
                if(ObjectUtil.nonNull(roundedDrawable)) {

                    boolean lessThan_JELLY_BEAN_MR1 = Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1;
                    Bitmap coverImg = roundedDrawable.getSourceBitmap();
                    bannerBackground = lessThan_JELLY_BEAN_MR1 ?
                            FastBlur.doBlur(coverImg, 70, true) :
                            BlurBuilder.blur(SummerApp.summerApp(), coverImg);
                    notifyPropertyChanged(BR.bannerBackground);
                }
            }
        }

        @Bindable
        public Bitmap getBannerBackground() {
            return bannerBackground;
        }

        public SummerBannerAdapter getBannerAdapter() {
            return bannerAdapter;
        }

        public class BoysBannerItemViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendBoysBannerItemBinding> {

            private RoundedImageView roundedImageView;

            private final ReferBinder<RoundedImageView> imageView = new ReferBinder<RoundedImageView>() {
                @Override
                protected void onBindView(RoundedImageView view) {
                    roundedImageView = view;
                }
            };

            private final AppAdvert appAdvert;

            public BoysBannerItemViewModel(AppAdvert appAdvert) {
                this.appAdvert = appAdvert;
            }

            @Override
            public void bindVariable(LayoutHomeRecommendBoysBannerItemBinding viewBind) {
                viewBind.setBoysBannerItemViewModel(this);
            }

            public void doAction() {
                appAdvert.doAction();
            }

            @Bindable
            public String getAdvertImage() {
                return appAdvert.advertImage;
            }

            public ReferBinder getImageView() {
                return imageView;
            }

        }

    }

}
